import os
import inspect
from PyQt4.QtCore import SIGNAL
import sys

from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import Qt
from PyQt4.QtCore import QObject, QString
from PyQt4.QtGui import qApp
from PyQt4 import QtCore

from settings import set
from settings import get
from settings import save
from settings import hasOption
from settings import hasSection
from settings import addSection


class PluginManager:
    def __init__(self):
        self.pluginDir = 'plugins'
        self.exclusions = ['__init__.py']
        self.modules = []
        self.plugins = []


    def loadPlugins(self, mw, playlist, player, informer):
        self.mw = mw

        # search for plugins
        execDir = os.path.dirname(sys.argv[0])
        for fname in os.listdir(execDir + os.sep + 'phonik' + os.sep + self.pluginDir):


        # only py files and not exclusions
            if fname.endswith(".py") and fname not in self.exclusions:

            # remove '.py'
                moduleName = fname[: -3]

                #print "Load module %s" % moduleName

                # load module and put it in the list

                packageObj = __import__('phonik.' + self.pluginDir + "." + moduleName)
                self.modules.append(moduleName)

                #            else:
                #                    print "Skip " + fname

                #         traverse loaded modules

        for moduleName in self.modules:
            moduleObj = getattr(packageObj.plugins, moduleName)

            for elem in dir(moduleObj):
                obj = getattr(moduleObj, elem)

                # is it class?
                if inspect.isclass(obj):

                # inherited from plugin?
                    if issubclass(obj, Plugin):
                        plugin = obj()
                        plugin.setPlayer(player)
                        plugin.setInformer(informer)
                        plugin.setMainWindow(mw)

                        if issubclass(obj, UIPlugin):
                            plugin.setPlaylist(playlist)

                        self.plugins.append(plugin)
                        plugin.load()

                        #print self.mw.roomsBox.findText(QString.fromUtf8(get('active-plugin')))
        self.mw.roomsBox.setCurrentIndex(int(get('active-plugin', 0)))


    def unloadPlugins(self):
        for p in self.plugins:
            p.unload()

        set('active-plugin', self.mw.roomsBox.currentIndex())


class Plugin(QObject):
    pluginName = ''

    def __init__(self):
        QObject.__init__(self)


    def load(self):
        pass


    def unload(self):
        pass


    def set(self, key, value):
        if not hasSection(self.getName()):
            addSection(self.getName())

        set(key, value, self.getName())


    def get(self, key, default=''):
        return get(key, name=self.getName(), default=default)


    def hasOption(self, key):
        return hasOption(key, self.getName())


    def setPlayer(self, player):
        self.player = player


    def setInformer(self, informer):
        self.informer = informer


    def setName(self, name):
        self.pluginName = name
        self.pluginNameTr = self.tr(name)


    def firstRun(self):
        return not hasSection(self.getName())


    def getName(self):
        return self.pluginName

    def getMenuBar(self):
        return self.mw.menubar

    def setMainWindow(self, mw):
        self.mw = mw

    def getMainWindow(self):
        return self.mw


class UIPlugin(Plugin):
    def __init__(self):
        Plugin.__init__(self)


    def setPlaylist(self, playlist):
        self.playlist = playlist


    def addPage(self, widget):
        self.mw.stackedWidget.addWidget(widget)
        self.mw.roomsBox.addItem(self.pluginNameTr)

